package com.weibo.view.activity;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import weibo4j.Oauth;
import weibo4j.http.AccessToken;
import weibo4j.model.WeiboException;
import weibo4j.util.BareBonesBrowserLaunch;

import com.weibo.action.OAuthAction;
import com.weibo.util.ImageUtil;
import com.weibo.util.MBox;
import com.weibo.util.MathUtil;

public class LoginActivity extends Dialog {

	public static final int width  = 450;
	public static final int height = 250;
	
	protected Object result;
	protected Shell shell;
	private Text code;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public LoginActivity(Shell parent, int style) {
		super(parent, style);
		setText("新浪认证");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setLocation(new Point((MathUtil.getscreenw()-width)/2, (MathUtil.getscreenh()-height)/2));
		shell.setSize(width, height);
		shell.setText(getText());
		
		Button oauth = new Button(shell, SWT.FLAT|SWT.CENTER|SWT.BORDER);
		oauth.setImage(ImageUtil.getImage(shell.getDisplay(), "48.png"));
		oauth.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Oauth oauth = new Oauth();
				try {
					BareBonesBrowserLaunch.openURL(oauth.authorize("code"));
				} catch (WeiboException e1) {
					e1.printStackTrace();
				}
			}
		});
		oauth.setBounds(10, 10, 424, 60);
		oauth.setToolTipText("新浪认证");
		
		Label lblNewLabel_1 = new Label(shell, SWT.CENTER);
		lblNewLabel_1.setBounds(10, 76, 424, 17);
		lblNewLabel_1.setText("code为认证后显示页面中url的?code=之后的指");
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(10, 112, 61, 17);
		lblNewLabel.setText("code：");
		
		code = new Text(shell, SWT.BORDER);
		code.setBounds(92, 109, 342, 23);
		
		Button reset = new Button(shell, SWT.NONE|SWT.BORDER);
		reset.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				code.setText("");
			}
		});
		reset.setBounds(10, 151, 202, 35);
		reset.setText("重新填写");
		
		Button submit = new Button(shell, SWT.NONE|SWT.BORDER);
		submit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					if(null==code||"".equals(code.getText())){
						MBox.info(getParent(), "请输入得到的code!");
						return ;
					}
					if(code.getText().trim().length()!=32){
						MBox.info(getParent(), "输入的code格式不对!");
						return ;
					}
					Oauth oauth = new Oauth();
					AccessToken token = oauth.getAccessTokenByCode(code.getText());
					OAuthAction.getInstance().setAccessToken(token);
					LoginActivity.this.shell.dispose();
					WeiboActivity activity = new WeiboActivity();
					activity.open();
				} catch (WeiboException e1) {
					e1.printStackTrace();
				}
			}
		});
		submit.setBounds(218, 151, 216, 35);
		submit.setText("提交Code");
	}
}
